
import json
import logging
import os , concurrent.futures as ft



class Manager(object):
    cfg =None

    def __init__(self) -> None:
        try:
            conf = json.loads(open('config.json'))
            self.cfg = {
                'website': self.read_websites(conf['website']),
                'dics': self.read_web_path(conf['dic_folder']),
                'thread_num': conf['thread_num']
            }

            self.pool = ft.ThreadPoolExecutor(max_workers = self.cfg['thread_num'])
        except  Exception as e :
            logging.error("manager __init__ errr: %s" % e)
        
    def start(self):
        try:
            for site in self.cfg['website']:
                w = Worker(site, self.cfg, self.pool)
                w.start()
        
        except Exception as e:
            logging.error("Manager start error: %s" % e)
        
    
    def read_web_path(self, diccfg):
        try: 
            dics = []
            doclist = os.listdir(diccfg)
            doclist.sort()

            for filename in doclist:
                filename = diccfg+'/'+filename
                with open(filename,'r') as f:
                    for i in f.readlines():
                        dics.append(i.strip())
            return dics
        except Exception as e:
            logging.error('dic error: %s, dicname: %s' % (e, filename))
        
    def read_websites(self, webcfg):
        try:
            websites = []
            with open(webcfg, 'r') as webs:
                for web in webs.readlines():
                    websites.append(web.strip())
                return websites

        except Exception as e:
            logging.error('read websites error: %s' % e)
        
    
def main():
    w = Manager()
    w.start()

if __name__ == '__main__': 
    main()
        


